package com.yimi.yinhepda.db.dao;

import android.content.Context;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.pda.ym.db.dao.IUnloadScanTableDao;
import com.pda.ym.utils.Constants;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.pda.ym.db.table.UnloadScanTable;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.sql.SQLException;
import java.util.List;

import static com.yimi.yinhepda.utils.Common.FEN_BO_ZI_TI_SCAN_TYPE;

/**
 * @author 000432
 * @date 2017/4/8
 * 装卸车扫描表数据库操作类
 */
@Route(path = Constants.YM_IUNLOADSCANDAO_IMPL)
public class UnloadScanTableDao extends BaseDao<UnloadScanTable, Long> implements IUnloadScanTableDao {
    private static final String TAG = UnloadScanTableDao.class.getSimpleName();

    public UnloadScanTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), UnloadScanTable.class);
    }

    /**
     * 查询是否已扫描
     *
     * @param task           批次号 主键
     * @param childWaybillNo 子单号
     */
    public boolean exist(String task, String childWaybillNo) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("childWaybillNo", childWaybillNo);
            where.and().eq("task", task);

            List<UnloadScanTable> listUnloadScanTable = queryBuilder.query();
            if (listUnloadScanTable != null && listUnloadScanTable.size() > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<UnloadScanTable> getTaskData(String task) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);

            List<UnloadScanTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadScanTable> getTaskWaybillNoData(String task) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadScanTable> getTaskWaybillNoData(String task, int operType) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);
            where.and().eq("operTypeCode", operType);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadScanTable> getTaskData(String task, String childWaybillNo) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);
            where.and().eq("childWaybillNo", childWaybillNo);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据配载单号删除
     *
     * @param operTypeCode 操作码:1 -装车，2- 卸车,3-取消串货记录 ，本地使用(3-离线装车，4-离线卸车),8-分拨自提扫描,9-配送扫描
     */
    public int deleteByChildWaybill(String stowageNo, String childWaybillNo, int operTypeCode) {
        int result = -1;
        try {
            DeleteBuilder<UnloadScanTable, Long> deleteBuilder = dao.deleteBuilder();
            Where where = deleteBuilder.where();
            where.eq("stowageNo", stowageNo);
            where.and().eq("childWaybillNo", childWaybillNo);
            where.and().eq("operTypeCode", operTypeCode);
            result = deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        }
        return result;
    }


    public List<UnloadScanTable> getStowageNoData(String stowageNo) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("stowageNo", stowageNo);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadScanTable> getDataByStowageAndType(String stowageNo, int operTypeCode) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("stowageNo", stowageNo);
            where.and().eq("operTypeCode", operTypeCode);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询满足条件的数量
     *
     * @param name  字段名
     * @param value 值
     */
    public long queryCount(String name, Object value, int operTypeCode) {
        long count = 0;
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();

            Where where = queryBuilder.where();
            where.eq(name, value);
            where.and().eq("operTypeCode", operTypeCode);
            count = queryBuilder.countOf();
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 查询未上传的数据
     */
    public List<UnloadScanTable> getUnUpload(String task, int operTypeCode) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("isUpload", 0);
            where.and().eq("operTypeCode", operTypeCode);
            where.and().eq("task", task);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadScanTable> getErrorData(Integer isUpload, String stowageNo, int operTypeCode) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("isUpload", isUpload);
            where.and().eq("operTypeCode", operTypeCode);
            where.and().eq("stowageNo", stowageNo);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取未上传数
     * @param task
     * @param operTypeCode
     * @return
     */
    public long getUnUploadData(String task, int operTypeCode) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            //不等于1，未上传
            where.ne("isUpload", 1);
            where.and().eq("operTypeCode", operTypeCode);
            where.and().eq("task", task);
            return queryBuilder.countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 查询满足条件的数量
     *
     * @param name  字段名
     * @param value 值
     */
    public long queryCount(String name, Object value, int operTypeCode, String userCode) {
        long count = 0;
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();

            Where where = queryBuilder.where();
            where.eq(name, value);
            where.and().eq("operTypeCode", operTypeCode);
            where.and().eq("operEmpCode", userCode);
            count = queryBuilder.countOf();
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 根据配载单号删除
     */
    public int deleteByTaskNo(String stowageNo) {
        int result = -1;
        try {
            DeleteBuilder<UnloadScanTable, Long> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq("stowageNo", stowageNo);
            result = deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        }
        return result;
    }

    /**
     * 根据task，配载单，运单号，获取已扫描的单号
     *
     * @param operTypeCode 1-装车，2-卸车
     * @param stowageNo    配载单
     * @param waybillNo    主运单号
     */
    public List<UnloadScanTable> getTaskWaybillNoData(Integer operTypeCode, String stowageNo, String waybillNo) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("operTypeCode", operTypeCode);
            where.and().eq("waybillNo", waybillNo);
            where.and().eq("stowageNo", stowageNo);
            List<UnloadScanTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据task，配载单，运单号，获取已扫描的单号
     */
    public UnloadScanTable getScanWayBillData(String stowageNo,int operTypeCode, String childWaybillNo) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("operTypeCode", operTypeCode);
            where.and().eq("childWaybillNo", childWaybillNo);
            where.and().eq("stowageNo", stowageNo);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取已扫且已上传的运单
     */
    public UnloadScanTable getScanChildWayBillData(String task, String stowageNo, String childWaybillNo) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);
            where.and().eq("childWaybillNo", childWaybillNo);
            where.and().eq("stowageNo", stowageNo);
            where.and().eq("isUpload", 1);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断子运单号是否存在
     */
    public boolean exitUploadChildWaybillNo(String userCode, String childWaybillNo, int operTypeCode) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("operEmpCode", userCode);
            where.and().eq("childWaybillNo", childWaybillNo);
            where.and().eq("operTypeCode", operTypeCode);
            where.and().eq("isUpload", 1);
            UnloadScanTable unloadScanTable = queryBuilder.queryForFirst();
            if (unloadScanTable != null) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据任务号判断子单号是否存在
     *
     * @param task           任务号
     * @param childWaybillNo
     * @param operTypeCode
     * @return
     */
    public boolean exitChildWaybillByTask(String task, String childWaybillNo, int operTypeCode) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);
            where.and().eq("childWaybillNo", childWaybillNo);
            where.and().eq("operTypeCode", operTypeCode);
            UnloadScanTable unloadScanTable = queryBuilder.queryForFirst();
            if (unloadScanTable != null) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据配载单号，操作类型，员工获取数据
     *
     * @param offlineTaskNo 离线任务号
     * @param operTypeCode  操作类型
     * @param userCode      员工
     */
    public List<UnloadScanTable> getDataByStowageAndType(String offlineTaskNo, int operTypeCode, String userCode) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("offlineTaskNo", offlineTaskNo);
            where.and().eq("operTypeCode", operTypeCode);
            where.and().eq("operEmpCode", userCode);
            List<UnloadScanTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据配载单号，操作类型，员工,子单号，获取数据
     *
     * @param offlineTaskNo 离线任务号
     * @param operTypeCode  操作类型
     * @param userCode      员工
     * @param result        子单号
     */
    public List<UnloadScanTable> getChildWaybillNo(String offlineTaskNo, int operTypeCode, String userCode, String result) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("offlineTaskNo", offlineTaskNo);
            where.and().eq("operTypeCode", operTypeCode);
            where.and().eq("operEmpCode", userCode);
            where.and().eq("childWaybillNo", result);
            List<UnloadScanTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据配载单号，操作类型，员工,主单号，获取数据
     *
     * @param stowageNo    配载单号
     * @param operTypeCode 操作类型
     * @param userCode     员工
     * @param result       子单号
     */
    public List<UnloadScanTable> getChildWaybillNoByWaybill(String stowageNo, int operTypeCode, String userCode, String result) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("stowageNo", stowageNo);
            where.and().eq("operTypeCode", operTypeCode);
            where.and().eq("operEmpCode", userCode);
            where.and().eq("waybillNo", result);
            List<UnloadScanTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据配载单号，操作类型，员工,主单号，获取数据
     *
     * @param offlineTaskNo 离线任务号
     * @param operTypeCode  操作类型
     * @param userCode      员工
     * @param result        主单号
     */
    public List<UnloadScanTable> getWaybillNo(String offlineTaskNo, int operTypeCode, String userCode, String result) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("offlineTaskNo", offlineTaskNo);
            where.and().eq("operTypeCode", operTypeCode);
            where.and().eq("operEmpCode", userCode);
            where.and().eq("waybillNo", result);
            List<UnloadScanTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取扫描的开始或结束时间
     *
     * @param stowageNo    配载单
     * @param operTypeCode 操作类型
     * @param userCode     员工编码
     * @param type         true-开始时间，false-结束时间
     */
    public String getScanEndTime(String stowageNo, int operTypeCode, String userCode, boolean type) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("stowageNo", stowageNo);
            where.and().eq("operTypeCode", operTypeCode);
            where.and().eq("operEmpCode", userCode);
            List<UnloadScanTable> listUnloadScanTable = queryBuilder.orderBy("scanTime", type).query();
            if (listUnloadScanTable != null && listUnloadScanTable.size() > 0) {
                return listUnloadScanTable.get(0).getScanTime();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadScanTable> getUcScanDataByTaskNo(List<String> stowgaeNos, Integer waybillType) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.in("stowageNo", stowgaeNos);
            where.and().eq("waybillType", waybillType);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询分拨自提扫描数据
     *
     * @param ziTiDeptCode 分拨自提部门
     * @param operEmpCode  操作人code
     * @return list
     */
    @NotNull
    public List<UnloadScanTable> findZiTiData(String ziTiDeptCode, String operEmpCode) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            //分拨自提操作类型
            where.eq("operTypeCode", FEN_BO_ZI_TI_SCAN_TYPE);
            where.and().eq("areaCode", ziTiDeptCode);
            where.and().eq("operEmpCode", operEmpCode);
            //未上传标志
            where.and().eq("isUpload", 0);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadScanTable> findZiTiByUpload(String ziTiDeptCode, String operEmpCode, Long limit) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder()
                    .orderBy("waybillNo", true).limit(limit);
            Where where = queryBuilder.where();
            //分拨自提操作类型
            where.eq("operTypeCode", FEN_BO_ZI_TI_SCAN_TYPE);
            where.and().eq("areaCode", ziTiDeptCode);
            where.and().eq("operEmpCode", operEmpCode);
            //已自提标志
            where.and().eq("isZiTi", 1);
            //无需更改标志
            where.or().eq("isZiTi", 2);
            //未上传标志
            where.and().eq("isUpload", 0);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除分拨自提数据
     */
    public String deleteZiTiData(String waybillNo, String ziTiDeptCode, String operEmpCode) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            //分拨自提操作类型
            where.eq("waybillNo", waybillNo);
            where.or().eq("childWaybillNo", waybillNo);
            where.and().eq("operTypeCode", FEN_BO_ZI_TI_SCAN_TYPE);
            where.and().eq("areaCode", ziTiDeptCode);
            where.and().eq("operEmpCode", operEmpCode);
            //未上传标志
            where.and().eq("isUpload", 0);
            List<UnloadScanTable> list = queryBuilder.query();
            if (list == null || list.isEmpty()) {
                return "该运单不存在或已撤销！";
            }
            int i = delete(list);
            if (i == -1) {
                return "撤销失败";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询未上传的数据
     */
    @Override
    public List<UnloadScanTable> getAllUnUploadData() {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("isUpload", 0);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void init(Context context) {

    }

    /**
     * 获取未上传的跨越数据
     */
    public List<UnloadScanTable> getKuaYueData(String task, String waybillNo, String kuaYueWaybillNo) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);
            where.and().eq("waybillNo", waybillNo);
            where.and().eq("kuayueWaybillNo", kuaYueWaybillNo);
            where.and().eq("isUpload", 0);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadScanTable> queryKuaYueScanedData(@Nullable String taskNo, int operTypeCode) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("isUpload", 0);
            where.and().eq("operTypeCode", operTypeCode);
            where.and().eq("task", taskNo);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据任务号查询本地已扫描的运单
     *
     * @param task 任务号
     */
    public List<UnloadScanTable> getChildWayBillData(String task) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("task", task);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<UnloadScanTable> getChildWayBillData(List<String> stowageNos, Integer operTypeCode) {
        try {
            QueryBuilder<UnloadScanTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.in("stowageNo", stowageNos);
            where.and().eq("operTypeCode", operTypeCode);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据运单号删除
     */
    public int deleteByWaybillNo(String waybillNo) {
        int result = -1;
        try {
            DeleteBuilder<UnloadScanTable, Long> deleteBuilder = dao.deleteBuilder();
            Where where = deleteBuilder.where();
            where.eq("childWaybillNo", waybillNo);
            where.or().eq("kuayueChildWaybillNo", waybillNo);
            result = deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        }
        return result;
    }
}
